home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / sccs.el < prev    next >
Encoding:
Text File  |  1995-02-05  |  29.1 KB  |  911 lines

  1. ;; sccs.el -- easy-to-use SCCS control from within Emacs
  2. ;;    @(#)sccs.el    3.5
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is distributed in the hope that it will be useful,
  7. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  8. ;; accepts responsibility to anyone for the consequences of using it
  9. ;; or for whether it serves any particular purpose or works at all,
  10. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  11. ;; License for full details.
  12.  
  13. ;; Everyone is granted permission to copy, modify and redistribute
  14. ;; GNU Emacs, but only under the conditions described in the
  15. ;; GNU Emacs General Public License.   A copy of this license is
  16. ;; supposed to have been given to you along with GNU Emacs so you
  17. ;; can know your rights and responsibilities.  It should be in a
  18. ;; file named COPYING.  Among other things, the copyright notice
  19. ;; and this notice must be preserved on all copies.
  20. ;;;
  21. ;;; Author: Eric S. Raymond (eric@snark.thyrsus.com).
  22. ;;;
  23. ;;; It is distantly derived from an rcs mode written by Ed Simpson
  24. ;;; ({decvax, seismo}!mcnc!duke!dukecdu!evs) in years gone by
  25. ;;; and revised at MIT's Project Athena.
  26. ;;; 
  27. ;;; Modified: Made to work for Lucid Emacs by persons who don't know SCCS.
  28. ;;; Modified: Ben Wing (Ben.Wing@eng.sun.com) -- fixed up and redid menus
  29. ;;;
  30.  
  31. ;; User options
  32.  
  33. (defvar sccs-bin-directory nil
  34.   "*Directory that holds the SCCS executables.
  35. Initialized automatically the first time you execute an SCCS command,
  36. if not already set.")
  37.  
  38. (defvar sccs-max-log-size 510
  39.   "*Maximum allowable size of an SCCS log message.")
  40. (defvar sccs-diff-command '("diff" "-c")
  41.   "*The command/flags list to be used in constructing SCCS diff commands.")
  42. (defvar sccs-headers-wanted '("\%\W\%")
  43.   "*SCCS header keywords to be inserted when sccs-insert-header is executed.")
  44. (defvar sccs-insert-static t
  45.   "*Insert a static character string when inserting SCCS headers in C mode.")
  46. (defvar sccs-mode-expert nil
  47.   "*Treat user as expert; suppress yes-no prompts on some things.")
  48.  
  49. ;; Vars the user doesn't need to know about.
  50.  
  51. (defvar sccs-log-entry-mode nil)
  52. (defvar sccs-current-major-version nil)
  53.  
  54. ;; Some helper functions
  55.  
  56. (defun sccs-name (file &optional letter)
  57.   "Return the sccs-file name corresponding to a given file."
  58.   (format "%sSCCS/%s.%s"
  59.       (concat (file-name-directory (expand-file-name file)))
  60.       (or letter "s")
  61.       (concat (file-name-nondirectory (expand-file-name file)))))
  62.  
  63. (defun sccs-lock-info (file index)
  64.    "Return the nth token in a file's SCCS-lock information."
  65.    (let
  66.        ((pfile (sccs-name file "p")))
  67.      (and (file-exists-p pfile)
  68.       (save-excursion
  69.         (find-file pfile)
  70.         (auto-save-mode nil)
  71.         (goto-char (point-min))
  72.         (replace-string " " "\n")
  73.         (goto-char (point-min))
  74.         (forward-line index)
  75.         (prog1
  76.         (buffer-substring (point) (progn (end-of-line) (point)))
  77.           (set-buffer-modified-p nil)
  78.           (kill-buffer (current-buffer)))
  79.         )
  80.       )
  81.      )
  82.    )
  83.  
  84. (defun sccs-locking-user (file)
  85.   "Return the name of the person currently holding a lock on FILE.
  86. Return nil if there is no such person."
  87.   (sccs-lock-info file 2)
  88.   )
  89.  
  90. (defun sccs-locked-revision (file)
  91.   "Return the revision number currently locked for FILE, nil if none such."
  92.   (sccs-lock-info file 1)
  93.   )
  94.  
  95. (defmacro error-occurred (&rest body)
  96.   (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
  97.  
  98. ;; There has *got* to be a better way to do this...
  99. (defmacro chmod (perms file)
  100.   (list 'call-process "chmod" nil nil nil perms file))
  101.  
  102. (defun sccs-save-vars (sid)
  103.   (save-excursion
  104.     (find-file "SCCS/emacs-vars.el")
  105.     (erase-buffer)
  106.     (insert "(setq sccs-current-major-version \"" sid "\")")
  107.     (basic-save-buffer)
  108.     )
  109.   )
  110.  
  111. (defun sccs-load-vars ()
  112.   (if (error-occurred (load-file "SCCS/emacs-vars.el"))
  113.       (setq sccs-current-major-version "1"))
  114. )
  115.  
  116. (defun sccs-init-bin-directory ()
  117.   (setq sccs-bin-directory
  118.     (cond ((file-executable-p "/usr/sccs/unget") "/usr/sccs")
  119.           ((file-executable-p "/usr/bin/unget") "/usr/bin")
  120.           ((file-directory-p "/usr/sccs") "/usr/sccs")
  121.           ((file-directory-p "/usr/bin/sccs") "/usr/bin/sccs")
  122.           (t "/usr/bin"))))
  123.  
  124. ;; The following functions do most of the real work
  125.  
  126. (defun sccs-get-version (file sid)
  127.    "For the given FILE, retrieve a copy of the version with given SID.
  128. The text is retrieved into a tempfile.  Return the tempfile name, or nil
  129. if no such version exists."
  130.   (let (oldversion vbuf)
  131.     (setq oldversion (sccs-name file (or sid "new")))
  132.     (setq vbuf (create-file-buffer oldversion))
  133.     (prog1
  134.     (if (not (error-occurred
  135.          (sccs-do-command vbuf "get" file
  136.                   (and sid (concat "-r" sid))
  137.                   "-p" "-s")))
  138.         (save-excursion
  139.           (set-buffer vbuf)
  140.           (write-region (point-min) (point-max) oldversion t 0)
  141.           oldversion)
  142.       )
  143.       (kill-buffer vbuf)
  144.       )
  145.     )
  146.   )
  147.  
  148. (defun sccs-mode-line (file)
  149.   "Set the mode line for an SCCS buffer.
  150. FILE is the file being visited to put in the modeline."
  151.   (setq mode-line-process
  152.     (if (file-exists-p (sccs-name file "p"))
  153.         (format " <SCCS: %s>" (sccs-locked-revision file))
  154.       ""))
  155.  
  156.     ; force update of frame
  157.     (save-excursion (set-buffer (other-buffer)))
  158.     (sit-for 0)
  159.     )
  160.  
  161. (defun sccs-do-command (buffer command file &rest flags)
  162.   "  Execute an SCCS command, notifying the user and checking for errors."
  163.   (setq file (expand-file-name file))
  164.   (message (format "Running %s on %s..." command file))
  165.   (or sccs-bin-directory (sccs-init-bin-directory))
  166.   (let ((status
  167.      (save-window-excursion
  168.        (set-buffer (get-buffer-create buffer))
  169.        (erase-buffer)
  170.        (while (and flags (not (car flags)))
  171.          (setq flags (cdr flags)))
  172.        (setq flags (append flags (and file (list (sccs-name file)))))
  173.        (let ((default-directory (file-name-directory (or file "./")))
  174.          (exec-path (cons sccs-bin-directory exec-path)))
  175.          (apply 'call-process command nil t nil flags)
  176.          )
  177.        (goto-char (point-max))
  178.        (previous-line 1)
  179.        (if (looking-at "ERROR")
  180.            (progn
  181.          (previous-line 1)
  182.          (print (cons command flags))
  183.          (next-line 1)
  184.          nil)
  185.          t))))
  186.     (if status
  187.     (message (format "Running %s...OK" command))
  188.       (pop-to-buffer buffer)
  189.       (error (format "Running %s...FAILED" command))))
  190.   (if file (sccs-mode-line file)))
  191.  
  192. (defun sccs-shell-command (command)
  193.   "Like shell-command except that the *Shell Command Output*buffer
  194. is created even if the command does not output anything"
  195.   (shell-command command)
  196.   (get-buffer-create "*Shell Command Output*"))
  197.  
  198. (defun sccs-tree-walk (func &rest optargs)
  199.   "Apply FUNC to each SCCS file under the default directory.
  200. If present, OPTARGS are also passed."
  201.   (sccs-shell-command (concat "/bin/ls -1 " default-directory "SCCS/s.*"))
  202.   (set-buffer "*Shell Command Output*")
  203.   (goto-char (point-min))
  204.   (replace-string "SCCS/s." "")
  205.   (goto-char (point-min))
  206.   (if (eobp)
  207.       (error "No SCCS files under %s" default-directory))
  208.   (while (not (eobp))
  209.     (let ((file (buffer-substring (point) (progn (end-of-line) (point)))))
  210.       (apply func file optargs)
  211.       )
  212.     (forward-line 1)
  213.     )
  214.   )
  215.  
  216. (defun sccs-init ()
  217.   (or (current-local-map) (use-local-map (make-sparse-keymap)))
  218.   (condition-case nil
  219.       ;; If C-c s is already defined by another mode, then we
  220.       ;; will get an error.  In that case, just don't do anything.
  221.       (progn
  222.     (define-key (current-local-map) "\C-cs?" 'describe-mode)
  223.     (define-key (current-local-map) "\C-csn" 'sccs)
  224.     (define-key (current-local-map) "\C-csm" 'sccs-register-file)
  225.     (define-key (current-local-map) "\C-csh" 'sccs-insert-headers)
  226.     (define-key (current-local-map) "\C-csd" 'sccs-revert-diff)
  227.     (define-key (current-local-map) "\C-csp" 'sccs-prs)
  228.     (define-key (current-local-map) "\C-csr" 'sccs-revert-buffer)
  229.     (define-key (current-local-map) "\C-cs\C-d" 'sccs-version-diff)
  230.     (define-key (current-local-map) "\C-cs\C-p" 'sccs-pending)
  231.     (define-key (current-local-map) "\C-cs\C-r" 'sccs-registered)
  232.     )
  233.     (error nil)))
  234.  
  235. ;; Here's the major entry point
  236.  
  237. (defun sccs (verbose)
  238.   "*Do the next logical SCCS operation on the file in the current buffer.
  239. You must have an SCCS subdirectory in the same directory as the file being
  240. operated on.
  241.    If the file is not already registered with SCCS, this does an admin -i
  242. followed by a get -e.
  243.    If the file is registered and not locked by anyone, this does a get -e.
  244.    If the file is registered and locked by the calling user, this pops up a
  245. buffer for creation of a log message, then does a delta -n on the file.
  246. A read-only copy of the changed file is left in place afterwards.
  247.    If the file is registered and locked by someone else, an error message is
  248. returned indicating who has locked it."
  249.   (interactive "P")
  250.   (sccs-init)
  251.   (if (buffer-file-name)
  252.       (let
  253.       (do-update revision owner
  254.              (file (buffer-file-name))
  255.              (sccs-file (sccs-name (buffer-file-name)))
  256.              (sccs-log-buf (get-buffer-create "*SCCS-Log*"))
  257.              (err-msg nil))
  258.  
  259.     ;; if there is no SCCS file corresponding, create one
  260.     (if (not (file-exists-p sccs-file))
  261.         (progn
  262.           (sccs-load-vars)
  263.           (sccs-admin 
  264.            file
  265.            (cond 
  266.         (verbose (read-string "Initial SID: "))
  267.         ((error-occurred (load-file "SCCS/emacs-vars.el")) "1")
  268.         (t sccs-current-major-version))
  269.            )
  270.           )
  271.       )
  272.  
  273.     (cond
  274.  
  275.      ;; if there is no lock on the file, assert one and get it
  276.      ((not (file-exists-p (sccs-name file "p")))
  277.       (progn
  278.         (sccs-get file t)
  279.         (revert-buffer nil t)
  280.         (sccs-mode-line file)
  281.         ))
  282.  
  283.      ;; a checked-out version exists, but the user may not own the lock
  284.      ((not (string-equal
  285.         (setq owner (sccs-locking-user file)) (user-login-name)))
  286.       (error "Sorry, %s has that file checked out" owner))
  287.  
  288.      ;; OK, user owns the lock on the file 
  289.      (t (progn
  290.  
  291.           ;; if so, give luser a chance to save before delta-ing.
  292.           (if (and (buffer-modified-p)
  293.                (or
  294.             sccs-mode-expert
  295.             (y-or-n-p (format "%s has been modified. Write it out? "
  296.                       (buffer-name)))))
  297.                (save-buffer))
  298.  
  299.           (setq revision (sccs-locked-revision file))
  300.  
  301.           ;; user may want to set nonstandard parameters
  302.           (if verbose
  303.           (if (or sccs-mode-expert (y-or-n-p 
  304.                (format "SID: %s  Change revision level? " revision)))
  305.               (setq revision (read-string "New revision level: "))))
  306.  
  307.           ;; OK, let's do the delta
  308.           (if
  309.           ;; this excursion returns t if the new version was saved OK
  310.           (save-window-excursion
  311.             (pop-to-buffer (get-buffer-create "*SCCS*"))
  312.             (erase-buffer)
  313.             (set-buffer-modified-p nil)
  314.             (sccs-mode)
  315.             (message 
  316.              "Enter log message. Type C-c C-c when done, C-c ? for help.")
  317.             (prog1
  318.             (and (not (error-occurred (recursive-edit)))
  319.                  (not (error-occurred (sccs-delta file revision))))
  320.               (setq buffer-file-name nil)
  321.               (bury-buffer "*SCCS*")))
  322.  
  323.           ;; if the save went OK do some post-checking
  324.           (if (buffer-modified-p)
  325.               (error
  326.                "Delta-ed version of file does not match buffer!")
  327.             (progn
  328.               ;; sccs-delta already turned off write-privileges on the
  329.               ;; file, let's not re-fetch it unless there's something
  330.               ;; in it that get would expand
  331.               ;;
  332.               ;; fooey on this.  You always need to refetch the
  333.               ;; file; otherwise weirdness will ensue when you're
  334.               ;; trying to do a make. --bpw
  335.               ; (if (sccs-check-headers)
  336.               (sccs-get file nil)
  337.               (revert-buffer nil t)
  338.               (sccs-mode-line file)
  339.               (run-hooks 'sccs-delta-ok)
  340.               )
  341.             ))))))
  342.     (error "There is no file associated with buffer %s" (buffer-name))))
  343.  
  344. (defun sccs-insert-last-log ()
  345.   "*Insert the log message of the last SCCS check in at point."
  346.   (interactive)
  347.   (insert-buffer sccs-log-buf))
  348.  
  349. ;;; These functions help the sccs entry point
  350.  
  351. (defun sccs-get (file writeable)
  352.   "Retrieve a copy of the latest delta of the given file."
  353.     (sccs-do-command "*SCCS*" "get" file (if writeable "-e")))
  354.  
  355. (defun sccs-admin (file sid)
  356.   "Checks a file into sccs.
  357. FILE is the unmodified name of the file.  SID should be the base-level sid to
  358. check it in under."
  359.   ; give a change to save the file if it's modified
  360.   (if (and (buffer-modified-p)
  361.        (y-or-n-p (format "%s has been modified. Write it out? "
  362.                  (buffer-name))))
  363.       (save-buffer))
  364.   (sccs-do-command "*SCCS*" "admin" file
  365.            (concat "-i" file) (concat "-r" sid))
  366.   (chmod "-w" file)
  367.   (if (sccs-check-headers)
  368.       (sccs-get file nil))    ;; expand SCCS headers
  369.   (revert-buffer nil t)
  370.   (sccs-mode-line file)
  371. )
  372.  
  373. (defun sccs-delta (file &optional rev comment)
  374.    "Delta the file specified by FILE.
  375. The optional argument REV may be a string specifying the new revision level
  376. \(if nil increment the current level). The file is retained with write
  377. permissions zeroed. COMMENT is a comment string; if omitted, the contents of
  378. the current buffer up to point becomes the comment for this delta."
  379.   (if (not comment)
  380.       (progn
  381.     (goto-char (point-max))
  382.     (if (not (bolp)) (newline))
  383.     (newline)
  384.     (setq comment (buffer-substring (point-min) (1- (point)))))
  385.     )
  386.   (sccs-do-command "*SCCS*" "delta" file "-n"
  387.        (if rev (format "-r%s" rev))
  388.        (format "-y%s" comment))
  389.   (chmod "-w" file))
  390.  
  391. (defun sccs-delta-abort ()
  392.   "Abort an SCCS delta command."
  393.   (interactive)
  394.   (if (or sccs-mode-expert (y-or-n-p "Abort the delta? "))
  395.       (progn
  396.     (delete-window)
  397.     (error "Delta aborted")))
  398.   )
  399.  
  400. (defun sccs-log-exit ()
  401.   "Leave the recursive edit of an SCCS log message."
  402.   (interactive)
  403.   (if (< (buffer-size) sccs-max-log-size)
  404.      (progn
  405.        (copy-to-buffer sccs-log-buf (point-min) (point-max))
  406.        (exit-recursive-edit)
  407.        (delete-window))
  408.      (progn
  409.        (goto-char sccs-max-log-size)
  410.        (error
  411.         "Log must be less than %d characters. Point is now at char %d."
  412.         sccs-max-log-size sccs-max-log-size)))
  413. )
  414.  
  415. ;; Additional entry points for examining version histories
  416.  
  417. (defun sccs-revert-diff (&rest flags)
  418.   "*Compare the version being edited with the last checked-in revision.
  419. Or, if given a prefix argument, with another specified revision."
  420.   (interactive)
  421.   (let (old file)
  422.     (if
  423.     (setq old (sccs-get-version (buffer-file-name) 
  424.                     (and
  425.                      current-prefix-arg
  426.                      (read-string "Revision to compare against: "))
  427.                     ))
  428.     (progn
  429.       (if (and (buffer-modified-p)
  430.            (or
  431.             sccs-mode-expert
  432.             (y-or-n-p (format "%s has been modified. Write it out? "
  433.                       (buffer-name)))))
  434.           (save-buffer))
  435.  
  436.       (setq file (buffer-file-name))
  437.       (set-buffer (get-buffer-create "*SCCS*"))
  438.       (erase-buffer)
  439.       (apply 'call-process (car sccs-diff-command) nil t nil
  440.          (append (cdr sccs-diff-command) flags (list old) (list file)))
  441.       (set-buffer-modified-p nil)
  442.       (goto-char (point-min))
  443.       (delete-file old)
  444.       (if (equal (point-min) (point-max))
  445.           (message (format "No changes to %s since last get." file))
  446.           (pop-to-buffer "*SCCS*")
  447.           )
  448.       )
  449.       )
  450.     )
  451.   )
  452.  
  453. (defun sccs-prs ()
  454.   "*List the SCCS log of the current buffer in an emacs window."
  455.   (interactive)
  456.   (if (and buffer-file-name (file-exists-p (sccs-name buffer-file-name "s")))
  457.       (progn
  458.     (sccs-do-command "*SCCS*" "prs" buffer-file-name)
  459.     (pop-to-buffer (get-buffer-create "*SCCS*"))
  460.     )
  461.     (error "There is no SCCS file associated with this buffer")
  462.     )
  463.   )
  464.  
  465. (defun sccs-version-diff (file rel1 rel2)
  466.   "*For FILE, report diffs between two stored deltas REL1 and REL2 of it."
  467.   (interactive "fFile: \nsOlder version: \nsNewer version: ")
  468.   (if (string-equal rel1 "") (setq rel1 nil))
  469.   (if (string-equal rel2 "") (setq rel2 nil))
  470.   (set-buffer (get-buffer-create "*SCCS*"))
  471.   (erase-buffer)
  472.   (sccs-vdiff file rel1 rel2)
  473.   (set-buffer-modified-p nil)
  474.   (goto-char (point-min))
  475.   (if (equal (point-min) (point-max))
  476.       (message (format "No changes to %s between %s and %s." file rel1 rel2))
  477.     (pop-to-buffer "*SCCS*")
  478.     )
  479.   )
  480.  
  481. (defun sccs-vdiff (file rel1 rel2 &optional flags)
  482.   "Compare two deltas into the current buffer."
  483.   (let (vers1 vers2)
  484.     (and
  485.      (setq vers1 (sccs-get-version file rel1))
  486.      (setq vers2 (if rel2 (sccs-get-version file rel2) file))
  487. ;     (prog1
  488. ;     (save-excursion
  489. ;       (not (error-occurred
  490. ;         (call-process "prs" nil t t
  491. ;                   (sccs-name file))))
  492. ;     )
  493. ;       )
  494.      (unwind-protect
  495.      (apply 'call-process (car sccs-diff-command) nil t t
  496.         (append (cdr sccs-diff-command) flags (list vers1) (list vers2)))
  497.        (condition-case () (delete-file vers1) (error nil))
  498.        (if rel2
  499.        (condition-case () (delete-file vers2) (error nil)))
  500.        )
  501.      )
  502.     )
  503.   )
  504.  
  505. ;; SCCS header insertion code
  506.  
  507. (defun sccs-insert-headers ()
  508.   "*Insert headers for use with the Source Code Control System.
  509. Headers desired are inserted at the start of the buffer, and are pulled from 
  510. the variable sccs-headers-wanted"
  511.   (interactive)
  512.   (save-excursion
  513.     (save-restriction
  514.       (widen)
  515.       (if (or (not (sccs-check-headers))
  516.           (y-or-n-p "SCCS headers already exist.  Insert another set?"))
  517.       (progn
  518.          (goto-char (point-min))
  519.          (run-hooks 'sccs-insert-headers-hook)
  520.          (cond ((eq major-mode 'c-mode) (sccs-insert-c-header))
  521.            ((eq major-mode 'lisp-mode) (sccs-insert-lisp-header))
  522.            ((eq major-mode 'emacs-lisp-mode) (sccs-insert-lisp-header))
  523.            ((eq major-mode 'scheme-mode) (sccs-insert-lisp-header))
  524.            ((eq major-mode 'nroff-mode) (sccs-insert-nroff-header))
  525.            ((eq major-mode 'plain-tex-mode) (sccs-insert-tex-header))
  526.            ((eq major-mode 'texinfo-mode) (sccs-insert-texinfo-header))
  527.            (t (sccs-insert-generic-header))))))))
  528.  
  529. (defun sccs-insert-c-header ()
  530.   (let (st en)
  531.     (insert "/*\n")
  532.     (mapcar '(lambda (s)
  533.            (insert " *\t" s "\n"))
  534.         sccs-headers-wanted)
  535.     (insert " */\n\n")
  536.     (if (and sccs-insert-static 
  537.          (not (string-match "\\.h$" (buffer-file-name))))
  538.     (progn
  539.       (insert "#ifndef lint\n"
  540.           "static char *sccsid")
  541. ;;      (setq st (point))
  542. ;;      (insert (file-name-nondirectory (buffer-file-name)))
  543. ;;      (setq en (point))
  544. ;;      (subst-char-in-region st en ?. ?_)
  545.       (insert " = \"\%\W\%\";\n"
  546.           "#endif /* lint */\n\n")))
  547.     (run-hooks 'sccs-insert-c-header-hook)))
  548.  
  549. (defun sccs-insert-lisp-header ()
  550.   (mapcar '(lambda (s) 
  551.           (insert ";;;\t" s "\n"))
  552.       sccs-headers-wanted)
  553.   (insert "\n")
  554.   (run-hooks 'sccs-insert-lisp-header-hook))
  555.  
  556. (defun sccs-insert-nroff-header ()
  557.   (mapcar '(lambda (s) 
  558.           (insert ".\\\"\t" s "\n"))
  559.       sccs-headers-wanted)
  560.   (insert "\n")
  561.   (run-hooks 'sccs-insert-nroff-header-hook))
  562.  
  563. (defun sccs-insert-tex-header ()
  564.   (mapcar '(lambda (s) 
  565.           (insert "%%\t" s "\n"))
  566.       sccs-headers-wanted)
  567.   (insert "\n")
  568.   (run-hooks 'sccs-insert-tex-header-hook))
  569.  
  570. (defun sccs-insert-texinfo-header ()
  571.   (mapcar '(lambda (s) 
  572.           (insert "@comment\t" s "\n"))
  573.       sccs-headers-wanted)
  574.   (insert "\n")
  575.   (run-hooks 'sccs-insert-texinfo-header-hook))
  576.  
  577. (defun sccs-insert-generic-header ()
  578.   (let* ((comment-start-sccs (or comment-start "#"))
  579.      (comment-end-sccs (or comment-end ""))
  580.      (dont-insert-nl-p (string-match "\n" comment-end-sccs)))
  581.     (mapcar '(lambda (s)
  582.            (insert comment-start-sccs "\t" s ""
  583.                comment-end-sccs (if dont-insert-nl-p "" "\n")))
  584.       sccs-headers-wanted)
  585.   (insert comment-start-sccs comment-end-sccs (if dont-insert-nl-p "" "\n"))))
  586.  
  587. (defun sccs-check-headers ()
  588.   "Check if the current file has any SCCS headers in it."
  589.   (interactive)
  590.   (save-excursion
  591.     (goto-char (point-min))
  592.     (re-search-forward  "%[MIRLBSDHTEGUYFPQCZWA]%" (point-max) t)))
  593.  
  594. ;; Status-checking functions
  595.  
  596. (defun sccs-status (prefix legend)
  597.    "List all files underneath the current directory matching a prefix type."
  598.    (sccs-shell-command
  599.     (concat "/bin/ls -1 SCCS/" prefix ".*"))
  600.    (if
  601.        (save-excursion
  602.      (set-buffer "*Shell Command Output*")
  603.      (if (= (point-max) (point-min))
  604.          (not (message
  605.            "No files are currently %s under %s"
  606.            legend default-directory))
  607.        (progn
  608.          (goto-char (point-min))
  609.          (insert
  610.           "The following files are currently " legend
  611.           " under " default-directory ":\n")
  612.          (replace-string (format "SCCS/%s." prefix) "")
  613.          )
  614.        )
  615.      )
  616.        (pop-to-buffer "*Shell Command Output*")
  617.        )
  618.      )
  619.  
  620. (defun sccs-pending ()
  621.   "*List all files currently SCCS locked."
  622.   (interactive)
  623.   (sccs-status "p" "locked"))
  624.  
  625. (defun sccs-registered ()
  626.   "*List all files currently SCCS registered."
  627.   (interactive)
  628.   (sccs-status "s" "registered"))
  629.        
  630. (defun sccs-register-file (override)
  631.   "*Register the file visited by the current buffer into SCCS."
  632.   (interactive "P")
  633.   (if (file-exists-p (sccs-name (buffer-file-name)))
  634.       (error "This file is already registered into SCCS.")
  635.     (progn
  636.       (if (and (buffer-modified-p)
  637.            (or
  638.         sccs-mode-expert
  639.         (y-or-n-p (format "%s has been modified. Write it out? "
  640.                   (buffer-name)))))
  641.       (save-buffer))
  642.       (sccs-load-vars)
  643.       (sccs-admin 
  644.        (buffer-file-name)
  645.        (cond 
  646.     (override (read-string "Initial SID: "))
  647.     ((error-occurred (load-file "SCCS/emacs-vars.el")) "1")
  648.     (t sccs-current-major-version))
  649.        )
  650.       )
  651.     )
  652.   )
  653.  
  654. ;; Major functions for release-tracking and generation.
  655.  
  656. (defun sccs-release-diff (rel1 rel2)
  657.   "*Diff all files below default-directory between versions REL1 and REL2.
  658. The report goes to a shell output buffer which is popped to.  If REL2 is
  659. omitted or nil, the comparison is done against the most recent version."
  660.   (interactive "sOlder version: \nsNewer version: ")
  661.   (if (string-equal rel1 "") (setq rel1 nil))
  662.   (if (string-equal rel2 "") (setq rel2 nil))
  663.   (sccs-shell-command (concat
  664.                "/bin/ls -1 " default-directory "SCCS/s.*"
  665.                ))
  666.   (set-buffer "*Shell Command Output*")
  667.   (goto-char (point-min))
  668.   (replace-string "SCCS/s." "")
  669.   (goto-char (point-min))
  670.   (if (eobp)
  671.       (error "No SCCS files under %s" default-directory))
  672.   (let
  673.       ((sccsbuf (get-buffer-create "*SCCS*")))
  674.     (save-excursion
  675.       (set-buffer sccsbuf)
  676.       (erase-buffer)
  677.       (insert (format "Diffs from %s to %s.\n\n"
  678.               (or rel1 "current") (or rel2 "current"))))
  679.     (while (not (eobp))
  680.      (let ((file (buffer-substring (point) (progn (end-of-line) (point)))))
  681.        (save-excursion
  682.          (set-buffer sccsbuf)
  683.          (set-buffer-modified-p nil)
  684.  
  685.          (sccs-vdiff file rel1 rel2)
  686.          (if (buffer-modified-p)
  687.          (insert "\n"))
  688.          )
  689.        (forward-line 1)
  690.        )
  691.      )
  692.     (kill-buffer "*Shell Command Output*")
  693.     (pop-to-buffer sccsbuf)
  694.     (insert "\nEnd of diffs.\n")
  695.     (goto-char (point-min))
  696.     (replace-string (format "/SCCS/%s." rel1) "/")
  697.     (goto-char (point-min))
  698.     (replace-string (format "/SCCS/%s." rel2) "/new/")
  699.     (goto-char (point-min))
  700.     (replace-string "/SCCS/new." "/new/")
  701.     (goto-char (point-min))
  702.     (replace-regexp (concat "^*** " default-directory) "*** ")
  703.     (goto-char (point-min))
  704.     (replace-regexp (concat "^--- " default-directory) "--- ")
  705.     (goto-char (point-min))
  706.     (set-buffer-modified-p nil)
  707.     )
  708.   )
  709.  
  710. (defun sccs-dummy-delta (file sid)
  711.   "Make a dummy delta to the given FILE with the given SID."
  712.   (interactive "sFile: \nsRelease ID: ")
  713.   (if (not (sccs-locked-revision file))
  714.       (sccs-get file t))
  715.   ;; Grottiness alert -- to get around SCCS's obsessive second-guessing we
  716.   ;; have to mung the p-file
  717.   (save-excursion
  718.     (let ((pfile (sccs-name file "p")))
  719.       (chmod "u+w" pfile)
  720.       (find-file pfile)
  721.       (auto-save-mode nil)
  722.       (replace-regexp "^\\([0-9.]+\\) \\([0-9.]+\\)" (concat "\\1 " sid) t)
  723.       (write-region (point-min) (point-max) pfile t 0)
  724.       (chmod "u-w" pfile)
  725.       (set-buffer-modified-p nil)
  726.       (kill-buffer (current-buffer))
  727.       )
  728.     )
  729.   (sccs-delta file sid (concat "Release " sid))
  730.   (sccs-get file nil)
  731.   (sccs-save-vars sid)
  732.   )
  733.  
  734. (defun sccs-delta-release (sid)
  735.   "*Delta everything underneath the current directory to mark it as a release."
  736.   (interactive "sRelease: ")
  737.   (sccs-tree-walk 'sccs-dummy-delta sid)
  738.   (kill-buffer "*SCCS*")
  739.   )
  740.  
  741. ;; Miscellaneous other entry points
  742.  
  743. (defun sccs-revert-buffer ()
  744.   "*Revert the current buffer's file back to the last saved version."
  745.   (interactive)
  746.   (let ((file (buffer-file-name)))
  747.     (if (y-or-n-p (format "Revert file %s to last SCCS revision?" file))
  748.     (progn
  749.       (delete-file file)
  750.       (delete-file (sccs-name file "p"))
  751.       (rename-file (sccs-get-version file nil) file)
  752.       (chmod "-w" file)
  753.       (revert-buffer nil t)
  754.       (sccs-mode-line file)))))
  755.  
  756. (defun sccs-rename-file (old new)
  757.   "*Rename a file, taking its SCCS files with it."
  758.   (interactive "fOld name: \nFNew name: ")
  759.   (let ((owner (sccs-locking-user old)))
  760.     (if (and owner (not (string-equal owner (user-login-name))))
  761.     (error "Sorry, %s has that file checked out" owner))
  762.     )
  763.   (rename-file old new)
  764.   (if (file-exists-p (sccs-name old "p"))
  765.       (rename-file (sccs-name old "p") (sccs-name new "p")))
  766.   (if (file-exists-p (sccs-name old "s"))
  767.       (rename-file (sccs-name old "s") (sccs-name new "s")))
  768.   )
  769.  
  770. ;; Set up key bindings for SCCS use, e.g. while editing log messages
  771.  
  772. (defun sccs-mode ()
  773.   "Minor mode for driving the SCCS tools.
  774.  
  775. These bindings are added to the global keymap when you enter this mode:
  776. \\[sccs]    perform next logical SCCS operation (`sccs') on current file
  777. \\[sccs-register-file]        register current file into SCCS
  778. \\[sccs-insert-headers]        insert SCCS headers in current file
  779. \\[sccs-prs]        display change history of current file
  780. \\[sccs-revert-buffer]        revert buffer to last saved version
  781. \\[sccs-revert-diff]        show difference between buffer and last saved delta
  782. \\[sccs-pending]        show all files currently locked by any user in or below .
  783. \\[sccs-registered]        show all files registered into SCCS in or below .
  784. \\[sccs-version-diff]        show diffs between saved versions for all files in or below .
  785.  
  786. When you generate headers into a buffer using C-c h, the value of
  787. sccs-insert-headers-hook is called before insertion. If the file is
  788. recognized a C or Lisp source, sccs-insert-c-header-hook or
  789. sccs-insert-lisp-header-hook is called after insertion respectively.
  790.  
  791. While you are entering a change log message for a delta, the following
  792. additional bindings will be in effect.
  793.  
  794. \\[sccs-log-exit]        proceed with check in, ending log message entry
  795. \\[sccs-insert-last-log]        insert log message from last check-in
  796. \\[sccs-delta-abort]        abort this delta check-in
  797.  
  798. Entry to the change-log submode calls the value of text-mode-hook, then
  799. the value sccs-mode-hook.
  800.  
  801. Global user options:
  802.         sccs-mode-expert        suppresses some conformation prompts,
  803.                 notably for delta aborts and file saves.
  804.     sccs-max-log-size    specifies the maximum allowable size
  805.                 of a log message plus one.
  806.     sccs-diff-command    A list consisting of the command and flags
  807.                 to be used for generating context diffs.
  808.     sccs-headers-wanted    which %-keywords to insert when adding
  809.                 SCCS headers with C-c h
  810.     sccs-insert-static    if non-nil, SCCS keywords inserted in C files
  811.                 get stuffed in a static string area so that
  812.                 what(1) can see them in the compiled object
  813.                 code.
  814. "
  815.   (interactive)
  816.   (set-syntax-table text-mode-syntax-table)
  817.   (use-local-map sccs-log-entry-mode)
  818.   (setq local-abbrev-table text-mode-abbrev-table)
  819.   (setq major-mode 'sccs-mode)
  820.   (setq mode-name "SCCS Change Log Entry")
  821.   (run-hooks 'text-mode-hook 'sccs-mode-hook)
  822. )
  823.  
  824. ;; Initialization code, to be done just once at load-time
  825. (if sccs-log-entry-mode
  826.     nil
  827.   (setq sccs-log-entry-mode (make-sparse-keymap))
  828.   (define-key sccs-log-entry-mode "\C-ci" 'sccs-insert-last-log)
  829.   (define-key sccs-log-entry-mode "\C-c\C-i" 'sccs-insert-last-log)
  830.   (define-key sccs-log-entry-mode "\C-ca" 'sccs-delta-abort)
  831.   (define-key sccs-log-entry-mode "\C-c\C-a" 'sccs-delta-abort)
  832.   (define-key sccs-log-entry-mode "\C-c\C-c" 'sccs-log-exit)
  833.   (define-key sccs-log-entry-mode "\C-x\C-s" 'sccs-log-exit)
  834.   )
  835.  
  836.  
  837. ;;; Lucid Emacs support
  838.  
  839. (defconst sccs-menu
  840.   '("SCCS Commands"
  841.  
  842.     ["SCCS"            sccs            t    nil] ; C-c s n
  843.     ["Insert Headers"        sccs-insert-headers    t]         ; C-c s h
  844.     ["Archive History:"        sccs-prs        t    nil] ; C-c s p
  845.     ["Diffs from Archive:"    sccs-revert-diff    t    nil] ; C-c s d
  846.     ["Revert to Archive:"    sccs-revert-buffer    t    nil] ; C-c s r
  847.     "----"
  848.     ["Check In..."        sccs-dummy-delta    t]
  849.     ["Create Archive..."    sccs-register-file    t] ; C-c s h
  850.     ["Rename Archive..."    sccs-rename-file    t]
  851.     "----"
  852.     ["List Checked-Out Files"    sccs-pending        t]       ; C-c s C-p
  853.     ["List Registered Files"    sccs-registered        t]       ; C-c s C-r
  854.     ["Diff Directory"        sccs-release-diff    t]
  855.     ["Delta Directory"        sccs-delta-release    t]
  856.     ))
  857.  
  858. (progn
  859.   (delete-menu-item '("SCCS"))
  860.   (add-menu '() "SCCS" (cdr sccs-menu)))
  861.  
  862. (defun sccs-sensitize-menu ()
  863.   (let* ((rest (cdr (car (find-menu-item current-menubar '("SCCS")))))
  864.      (case-fold-search t)
  865.      (file (if buffer-file-name
  866.            (file-name-nondirectory buffer-file-name)
  867.          (buffer-name)))
  868.      (dir (file-name-directory
  869.            (if buffer-file-name buffer-file-name default-directory)))
  870.      (sccs-file (and buffer-file-name (sccs-name buffer-file-name)))
  871.      (known-p (and sccs-file (file-exists-p sccs-file)))
  872.      (checked-out-p (and known-p
  873.                  (file-exists-p (sccs-name buffer-file-name "p"))))
  874.      command
  875.      item)
  876.     (while rest
  877.       (setq item (car rest))
  878.       (if (not (vectorp item))
  879.       nil
  880.     (setq command (aref item 1))
  881.     (if (eq 'sccs command)
  882.         (aset item 0
  883.           (cond ((or (null sccs-file) (not known-p))
  884.              "Create Archive:")
  885.             ((not checked-out-p)
  886.              "Check Out")
  887.             (t
  888.              "Check In"))))
  889.     (cond
  890.      ((and (> (length item) 3)
  891.            (string-match "directory" (aref item 0)))
  892.       (aset item 3 dir))
  893.      ((> (length item) 3)
  894.       (aset item 3 file))
  895.      (t nil))
  896.     (aset item 2
  897.           (cond
  898.            ((memq command '(sccs-prs))
  899.         known-p)
  900.            ((memq command '(sccs-revert-diff sccs-revert-buffer))
  901.         checked-out-p)
  902.            (t))))
  903.     (setq rest (cdr rest))))
  904.   nil)
  905.  
  906. (add-hook 'activate-menubar-hook 'sccs-sensitize-menu)
  907.  
  908. (provide 'sccs)
  909.  
  910. ;; sccs.el ends here
  911.